﻿namespace DataGridViewPrint
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;

    internal class PrintDGV
    {
        private string address = "";
        private List<string> AvailableColumns = new List<string>();
        private int beizhu = 0;
        private Button CellButton;
        private CheckBox CellCheckBox;
        private ComboBox CellComboBox;
        private int CellHeight;
        private ArrayList ColumnLefts = new ArrayList();
        private ArrayList ColumnTypes = new ArrayList();
        private ArrayList ColumnWidths = new ArrayList();
        private string customer = "";
        private string desginer = "";
        private DataGridView dgv;
        private bool FitToPageWidth = true;
        private int h = 0;
        private int HeaderHeight = 0;
        private bool NewPage;
        private int PageNo;
        private string phone = "";
        private bool PrintAllRows = true;
        private PrintDocument printDoc = new PrintDocument();
        private string PrintTitle = "";
        private int row = 0;
        private int RowPos;
        private int RowsPerPage;
        private List<string> SelectedColumns = new List<string>();
        private StringFormat StrFormat;
        private StringFormat StrFormatComboBox;
        private string title = "";
        public static int total = 0;
        private int TotalWidth;

        private void DrawFooter(PrintPageEventArgs e, int RowsPerPage)
        {
            int count = 0;
            int num2 = 0;
            if (this.PrintAllRows)
            {
                if (this.dgv.Rows[this.dgv.Rows.Count - 1].IsNewRow)
                {
                    count = this.dgv.Rows.Count - 2;
                }
                else
                {
                    count = this.dgv.Rows.Count - 1;
                }
            }
            else
            {
                count = this.dgv.SelectedRows.Count;
            }
            if ((count % RowsPerPage) == 0)
            {
                num2 = count / RowsPerPage;
            }
            else
            {
                num2 = (count / RowsPerPage) + 1;
            }
            string s = " 第 " + this.PageNo.ToString() + " 页，共 " + num2.ToString() + " 页";
            e.Graphics.DrawString(s, this.dgv.Font, Brushes.Black, e.MarginBounds.Left + ((e.MarginBounds.Width - e.Graphics.MeasureString(s, this.dgv.Font, e.MarginBounds.Width).Width) / 2f), (float) ((e.MarginBounds.Top + e.MarginBounds.Height) + 0x1f));
        }

        public void Print_DataGridView(DataGridView dgv1)
        {
            try
            {
                try
                {
                    this.dgv = dgv1;
                    this.AvailableColumns.Clear();
                    foreach (DataGridViewColumn column in this.dgv.Columns)
                    {
                        if (column.Visible)
                        {
                            this.AvailableColumns.Add(column.HeaderText);
                        }
                    }
                    PrintOptions options = new PrintOptions(this.AvailableColumns, this.title);
                    if (options.ShowDialog() == DialogResult.OK)
                    {
                        this.PrintTitle = options.PrintTitle;
                        this.PrintAllRows = options.PrintAllRows;
                        this.FitToPageWidth = options.FitToPageWidth;
                        this.SelectedColumns = options.GetSelectedColumns();
                        PageSetupDialog dialog2 = new PageSetupDialog();
                        PageSettings settings = new PageSettings();
                        dialog2.Document = this.printDoc;
                        dialog2.PageSettings = this.printDoc.DefaultPageSettings;
                        dialog2.PageSettings = settings;
                        dialog2.ShowDialog();
                        this.printDoc.DefaultPageSettings.Landscape = dialog2.PageSettings.Landscape;
                        this.printDoc.DefaultPageSettings.PaperSize = dialog2.PageSettings.PaperSize;
                        this.RowsPerPage = 0;
                        PrintPreviewDialog dialog = new PrintPreviewDialog();
                        dialog.Document = this.printDoc;
                        this.printDoc.BeginPrint += new PrintEventHandler(this.PrintDoc_BeginPrint);
                        this.printDoc.PrintPage += new PrintPageEventHandler(this.PrintDoc_PrintPage);
                        dialog.ShowDialog();
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            finally
            {
            }
        }

        public void Print_DataGridView(DataGridView dgv1, string t, string cust, string p, string d, string a)
        {
            this.customer = cust;
            this.phone = p;
            this.desginer = d;
            this.address = a;
            this.title = t;
            total = 0;
            try
            {
                try
                {
                    this.dgv = dgv1;
                    this.AvailableColumns.Clear();
                    foreach (DataGridViewColumn column in this.dgv.Columns)
                    {
                        if (column.Visible)
                        {
                            this.AvailableColumns.Add(column.HeaderText);
                        }
                    }
                    PrintOptions options = new PrintOptions(this.AvailableColumns, this.title);
                    this.PrintTitle = this.title;
                    this.PrintAllRows = options.PrintAllRows;
                    this.FitToPageWidth = options.FitToPageWidth;
                    this.SelectedColumns = options.GetSelectedColumns();
                    PageSetupDialog dialog2 = new PageSetupDialog();
                    PageSettings settings = new PageSettings();
                    dialog2.Document = this.printDoc;
                    dialog2.PageSettings = this.printDoc.DefaultPageSettings;
                    dialog2.PageSettings = settings;
                    if (dialog2.ShowDialog() == DialogResult.OK)
                    {
                        this.printDoc.DefaultPageSettings.Landscape = dialog2.PageSettings.Landscape;
                        this.printDoc.DefaultPageSettings.PaperSize = dialog2.PageSettings.PaperSize;
                        this.RowsPerPage = 0;
                        PrintPreviewDialog dialog = new PrintPreviewDialog();
                        dialog.Document = this.printDoc;
                        dialog.Width = 680;
                        dialog.Height = 700;
                        this.printDoc.BeginPrint += new PrintEventHandler(this.PrintDoc_BeginPrint);
                        this.printDoc.PrintPage += new PrintPageEventHandler(this.PrintDoc_PrintPage);
                        dialog.ShowDialog();
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            finally
            {
            }
        }

        private void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
        {
            try
            {
                this.StrFormat = new StringFormat();
                this.StrFormat.Alignment = StringAlignment.Near;
                this.StrFormat.LineAlignment = StringAlignment.Center;
                this.StrFormat.Trimming = StringTrimming.EllipsisCharacter;
                this.StrFormatComboBox = new StringFormat();
                this.StrFormatComboBox.LineAlignment = StringAlignment.Center;
                this.StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap;
                this.StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter;
                this.ColumnLefts.Clear();
                this.ColumnWidths.Clear();
                this.ColumnTypes.Clear();
                this.CellHeight = 0;
                this.RowsPerPage = 0;
                this.CellButton = new Button();
                this.CellCheckBox = new CheckBox();
                this.CellComboBox = new ComboBox();
                this.TotalWidth = 0;
                foreach (DataGridViewColumn column in this.dgv.Columns)
                {
                    if (column.Visible && this.SelectedColumns.Contains(column.HeaderText))
                    {
                        this.TotalWidth += column.Width;
                    }
                }
                this.PageNo = 1;
                this.NewPage = true;
                this.RowPos = 0;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "打印异常2", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            int num = 0;
            int top = e.MarginBounds.Top;
            int num5 = e.MarginBounds.Left - 70;
            try
            {
                if (this.PageNo == 1)
                {
                    foreach (DataGridViewColumn column in this.dgv.Columns)
                    {
                        if (column.Visible && this.SelectedColumns.Contains(column.HeaderText))
                        {
                            int num2;
                            if (this.FitToPageWidth)
                            {
                                num2 = ((int) Math.Floor((double) (((((double) column.Width) / ((double) this.TotalWidth)) * this.TotalWidth) * (((double) e.MarginBounds.Width) / ((double) this.TotalWidth))))) + 10;
                            }
                            else
                            {
                                num2 = column.Width + 10;
                            }
                            this.HeaderHeight = ((int) e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, num2).Height) + 11;
                            this.ColumnLefts.Add(num5);
                            this.ColumnWidths.Add(num2);
                            this.ColumnTypes.Add(column.GetType());
                            num5 += num2;
                            num += num2;
                        }
                    }
                }
                this.row = 0;
                while (this.RowPos <= (this.dgv.Rows.Count - 1))
                {
                    int num3;
                    Font font2;
                    Font font3;
                    DataGridViewRow row = this.dgv.Rows[this.RowPos];
                    if (row.IsNewRow || (!this.PrintAllRows && !row.Selected))
                    {
                        this.RowPos++;
                        continue;
                    }
                    if (!(!this.dgv.Rows[this.row].Cells[0].Value.ToString().Trim().Contains("、") || this.dgv.Rows[this.row].Cells[0].Value.ToString().Trim().Contains("(")))
                    {
                        this.CellHeight = row.Height + 5;
                    }
                    else
                    {
                        this.CellHeight = row.Height + 20;
                    }
                    if ((top + this.CellHeight) >= (e.MarginBounds.Height + e.MarginBounds.Top))
                    {
                        this.DrawFooter(e, this.RowsPerPage);
                        this.NewPage = true;
                        this.PageNo++;
                        e.HasMorePages = true;
                        return;
                    }
                    if (this.NewPage && (this.PageNo == 1))
                    {
                        Font prototype = new Font("Arial", 18f, FontStyle.Bold);
                        font2 = new Font("Arial", 12f, FontStyle.Bold);
                        font3 = new Font("Arial", 10f, FontStyle.Regular);
                        if (this.PrintTitle.Trim().Equals(""))
                        {
                            e.Graphics.DrawString("万泰建筑装饰工程有限公司", new Font(prototype, FontStyle.Bold), Brushes.Black, (float) (e.MarginBounds.Left + 120), (e.MarginBounds.Top - e.Graphics.MeasureString("万泰建筑装饰工程有限公司", new Font(prototype, FontStyle.Bold), e.MarginBounds.Width).Height) + 10f);
                        }
                        else
                        {
                            e.Graphics.DrawString("万泰建筑装饰工程有限公司", new Font(prototype, FontStyle.Bold), Brushes.Black, (float) (e.MarginBounds.Left + 120), (e.MarginBounds.Top - e.Graphics.MeasureString("万泰建筑装饰工程有限公司", new Font(prototype, FontStyle.Bold), e.MarginBounds.Width).Height) - 15f);
                        }
                        e.Graphics.DrawString(this.PrintTitle, new Font(font2, FontStyle.Bold), Brushes.Black, (float) (e.MarginBounds.Left + 150), (e.MarginBounds.Top - e.Graphics.MeasureString(this.PrintTitle, new Font(font2, FontStyle.Bold), e.MarginBounds.Width).Height) + 30f);
                        string s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
                        e.Graphics.DrawString(this.customer, new Font(font3, FontStyle.Bold), Brushes.Black, (float) (e.MarginBounds.Left - 70), ((e.MarginBounds.Top + 0x19) - e.Graphics.MeasureString(this.customer, new Font(font3, FontStyle.Bold), e.MarginBounds.Width).Height) + 50f);
                        e.Graphics.DrawString(this.phone, new Font(font3, FontStyle.Bold), Brushes.Black, (float) ((e.MarginBounds.Left + 260) - 70), ((e.MarginBounds.Top + 0x19) - e.Graphics.MeasureString(this.phone, new Font(font3, FontStyle.Bold), e.MarginBounds.Width).Height) + 50f);
                        e.Graphics.DrawString(this.desginer, new Font(font3, FontStyle.Bold), Brushes.Black, (float) ((e.MarginBounds.Left + 550) - 70), ((e.MarginBounds.Top + 0x19) - e.Graphics.MeasureString(this.desginer, new Font(font3, FontStyle.Bold), e.MarginBounds.Width).Height) + 50f);
                        e.Graphics.DrawString(this.address, new Font(font3, FontStyle.Bold), Brushes.Black, (float) (e.MarginBounds.Left - 70), ((e.MarginBounds.Top + 50) - e.Graphics.MeasureString(this.address, new Font(font3, FontStyle.Bold), e.MarginBounds.Width).Height) + 50f);
                        e.Graphics.DrawString(s, new Font(font3, FontStyle.Bold), Brushes.Black, (float) ((e.MarginBounds.Left + 800) - 70), ((e.MarginBounds.Top + 50) - e.Graphics.MeasureString(s, new Font(font3, FontStyle.Bold), e.MarginBounds.Width).Height) + 50f);
                        top = e.MarginBounds.Top + 110;
                        num3 = 0;
                        foreach (DataGridViewColumn column in this.dgv.Columns)
                        {
                            if (!column.Visible || !this.SelectedColumns.Contains(column.HeaderText))
                            {
                                continue;
                            }
                            e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), new Rectangle((int) this.ColumnLefts[num3], top, (int) this.ColumnWidths[num3], this.HeaderHeight));
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int) this.ColumnLefts[num3], top, (int) this.ColumnWidths[num3], this.HeaderHeight));
                            if (num3 == 0)
                            {
                                e.Graphics.DrawString(column.HeaderText, font2, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 0x23), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.HeaderHeight), this.StrFormat);
                            }
                            else if (num3 == 1)
                            {
                                e.Graphics.DrawString(column.HeaderText, font2, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 100), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.HeaderHeight), this.StrFormat);
                            }
                            else
                            {
                                e.Graphics.DrawString(column.HeaderText, font2, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 15), (float) top, (float) (((int) this.ColumnWidths[num3]) - 10), (float) this.HeaderHeight), this.StrFormat);
                            }
                            num3++;
                        }
                        this.NewPage = false;
                        top += this.HeaderHeight;
                    }
                    num3 = 0;
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        font3 = new Font("Arial", 8f, FontStyle.Regular);
                        Font font = new Font("Arial", 7.9f, FontStyle.Regular);
                        font2 = new Font("Arial", 7.9f, FontStyle.Bold);
                        if (cell.OwningColumn.Visible && this.SelectedColumns.Contains(cell.OwningColumn.HeaderText))
                        {
                            if (((!cell.FormattedValue.ToString().Trim().Equals("") && cell.FormattedValue.ToString().Trim().Contains("、")) && !cell.FormattedValue.ToString().Trim().Contains("(")) && (num3 == 0))
                            {
                                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle((int) this.ColumnLefts[0], top, 0x2fd, this.CellHeight));
                            }
                            if ((((System.Type) this.ColumnTypes[num3]).Name == "DataGridViewTextBoxColumn") || (((System.Type) this.ColumnTypes[num3]).Name == "DataGridViewLinkColumn"))
                            {
                                if (cell.FormattedValue.ToString().Trim().Equals("备注") && (num3 == 0))
                                {
                                    e.Graphics.DrawString("  ", font3, new SolidBrush(Color.Black), new RectangleF((float) ((int) this.ColumnLefts[1]), (float) top, (float) ((int) this.ColumnWidths[1]), (float) this.CellHeight), this.StrFormat);
                                    string str2 = this.dgv.Rows[this.RowPos].Cells[1].Value.ToString();
                                    e.Graphics.DrawString(str2, font3, new SolidBrush(Color.Black), new RectangleF(60f, top - 148f, 740f, (float) this.CellHeight), this.StrFormat);
                                }
                                else if ((!cell.FormattedValue.ToString().Trim().Equals("") && cell.FormattedValue.ToString().Trim().Contains("、")) && !cell.FormattedValue.ToString().Trim().Contains("("))
                                {
                                    if ((((num3 == 2) || (num3 == 3)) || (num3 == 4)) || (num3 == 5))
                                    {
                                        e.Graphics.DrawString(cell.FormattedValue.ToString(), font3, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 12), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.CellHeight), this.StrFormat);
                                    }
                                    else if (num3 == 1)
                                    {
                                        if (this.RowPos != (this.dgv.Rows.Count - 1))
                                        {
                                            e.Graphics.DrawString(cell.FormattedValue.ToString(), font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) ((int) this.ColumnLefts[num3]), (float) top, (float) ((int) this.ColumnWidths[num3]), this.CellHeight + 5f), this.StrFormat);
                                        }
                                    }
                                    else
                                    {
                                        e.Graphics.DrawString(cell.FormattedValue.ToString(), font2, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) ((int) this.ColumnLefts[num3]), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.CellHeight), this.StrFormat);
                                    }
                                }
                                else if ((((num3 == 2) || (num3 == 3)) || (num3 == 4)) || (num3 == 5))
                                {
                                    e.Graphics.DrawString(cell.FormattedValue.ToString(), font3, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 12), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.CellHeight), this.StrFormat);
                                }
                                else if (num3 == 1)
                                {
                                    if (this.RowPos != (this.dgv.Rows.Count - 1))
                                    {
                                        e.Graphics.DrawString(cell.FormattedValue.ToString(), font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 5), (float) top, (float) (((int) this.ColumnWidths[num3]) - 5), (float) this.CellHeight), this.StrFormat);
                                    }
                                }
                                else if (!cell.FormattedValue.ToString().Trim().Equals(""))
                                {
                                    e.Graphics.DrawString(cell.FormattedValue.ToString().Substring(cell.FormattedValue.ToString().Trim().IndexOf(')') + 1), font3, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 30), (float) top, (float) (((int) this.ColumnWidths[num3]) - 0x16), (float) this.CellHeight), this.StrFormat);
                                    e.Graphics.DrawString(cell.FormattedValue.ToString().Substring(1, cell.FormattedValue.ToString().Trim().IndexOf(')') - 1), font3, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 5), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.CellHeight), this.StrFormat);
                                    e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int) this.ColumnLefts[num3], top, ((int) this.ColumnWidths[num3]) - 120, this.CellHeight));
                                }
                                else
                                {
                                    e.Graphics.DrawString(cell.FormattedValue.ToString(), font3, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF((float) (((int) this.ColumnLefts[num3]) + 30), (float) top, (float) ((int) this.ColumnWidths[num3]), (float) this.CellHeight), this.StrFormat);
                                }
                            }
                            if ((this.dgv.Rows.Count - 1) != this.RowPos)
                            {
                                e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int) this.ColumnLefts[num3], top, (int) this.ColumnWidths[num3], this.CellHeight));
                            }
                            num3++;
                        }
                    }
                    top += this.CellHeight;
                    this.RowPos++;
                    if (this.PageNo == 1)
                    {
                        this.RowsPerPage++;
                    }
                    this.row++;
                    total++;
                }
                if (this.RowsPerPage != 0)
                {
                    this.DrawFooter(e, this.RowsPerPage);
                    e.HasMorePages = false;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "打印异常", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void remark(PrintPageEventArgs e, int t)
        {
            int count = 0;
            int num2 = 0;
            if (this.PrintAllRows)
            {
                count = this.dgv.Rows.Count - 1;
            }
            else
            {
                count = this.dgv.SelectedRows.Count;
            }
            if ((count % this.RowsPerPage) == 0)
            {
                num2 = count / this.RowsPerPage;
            }
            else
            {
                num2 = (count / this.RowsPerPage) + 1;
            }
            if (this.PageNo == num2)
            {
                e.Graphics.DrawString("1、所有物业押金及各种形式的管理款项敬请您承担，我方承担因施工违反物业规定而扣出的部分费用。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 20));
                e.Graphics.DrawString("2、施工中如有漏报项目或增加项目，则按实际施工项目结算工程款。如有计算错误则按实际发生量为准。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 0x23));
                e.Graphics.DrawString("3、以上项目都不含五金件。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 50));
                e.Graphics.DrawString("4、所有施工项目的工程量以竣工后实际测量为准。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 0x41));
                e.Graphics.DrawString("5、乳胶漆标准为三色(含白色),如超出三色,加收150元/色。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 80));
                e.Graphics.DrawString("6、水电路在中期时按实际发生量结算。电路施工单价：明走管20元/米；砖墙暗走管25元/米；混凝土暗走管35元/米；线盒10元/个，PPR暗走管90元/米。", this.dgv.Font, Brushes.Black, (float) (e.MarginBounds.Left - 60), (float) (t + 0x5f));
            }
            else
            {
                string s = " 第 " + this.PageNo.ToString() + " 页，共 " + num2.ToString() + " 页";
                e.Graphics.DrawString(s, this.dgv.Font, Brushes.Black, e.MarginBounds.Left + ((e.MarginBounds.Width - e.Graphics.MeasureString(s, this.dgv.Font, e.MarginBounds.Width).Width) / 2f), (float) ((e.MarginBounds.Top + e.MarginBounds.Height) + 0x1f));
            }
        }
    }
}

